package com.eqdd.yiqidian.ui.tongxunlu.tag;

import android.databinding.DataBindingUtil;
import android.os.Parcelable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;

import com.eqdd.yiqidian.AddTagActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.FriendListResultBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.utils.ItemDecorationUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.NewFlowLayoutManager;
import com.javonlee.dragpointview.OnPointDragListener;
import com.javonlee.dragpointview.view.AbsDragPointView;
import com.javonlee.dragpointview.view.DragPointView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;

public class EditTagActivity extends BaseActivity {
    private RecyclerViewCustom dataBinding;
    private SlimAdapterEx<FriendListResultBean> slimAdapterEx;
    private ArrayList<FriendListResultBean> chooseData;
    private String lableid;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);
        initTopTitleBar(View.VISIBLE, "编辑人员");
        initTopRightText("保存", v -> {
            if (!TextUtils.isEmpty(getLableds())) {
                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.REMOVE_TAG_PERSON)
                        .params("owner", user.getGuid())
                        .params("labelid", lableid)
                        .params("labelfriends", getLableds())
                        .execute(new DialogCallBack<HttpResult>(EditTagActivity.this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    setResult(Config.SUCCESS);
                                    finish();
                                }
                            }
                        });
            } else {
                ToastUtil.showShort("至少选择一人");
            }
        });

    }

    private String getLableds() {
        StringBuilder stringBuilder = new StringBuilder();
        Flowable.fromIterable(slimAdapterEx.getData())
                .subscribe(bean -> {
                    if (bean.isCheck()) {
                        stringBuilder.append(bean.getGuid()).append(";");
                    }
                });
        if (TextUtils.isEmpty(stringBuilder)) {
            return "";
        } else {
            return stringBuilder.toString();
        }
    }

    @Override
    public void initData() {
        chooseData = getIntent().getParcelableArrayListExtra("list");
        lableid = getIntent().getStringExtra(Config.LABLEID);
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_47, new SlimInjector<FriendListResultBean>() {
            @Override
            public void onInject(FriendListResultBean data, IViewInjector injector) {
                injector.enable(R.id.checkbox, data.isEnable())
                        .checked(R.id.checkbox, data.isCheck())
                        .imageCircle(R.id.iv_head, data.getIphoto())
                        .text(R.id.tv_content, data.getUpname());
            }
        }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {
                    if (slimAdapterEx.getDataItem(position).isEnable()) {
                        slimAdapterEx.getDataItem(position).setCheck(!slimAdapterEx.getDataItem(position).isCheck());
                        slimAdapterEx.notifyItemChanged(position);
                    }
                });


    }

    @Override
    public void setView() {
        OkGo.<HttpResult<List<FriendListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.FRIEND_LIST_INFO)
                .params("userGuid", user.getGuid())
                .execute(new DialogCallBack<HttpResult<List<FriendListResultBean>>>(this) {
                    @Override
                    public void onSuccess(Response<HttpResult<List<FriendListResultBean>>> response) {
                        List<FriendListResultBean> items = response.body().getItems();
                        if (response.body().getStatus() == 200) {
                            ItemDecorationUtil.openFloatDeco(EditTagActivity.this, dataBinding.recyclerView, response.body().getItems(), "upname");
                            Flowable.fromIterable(chooseData)
                                    .subscribe(bean -> {
                                        bean.setCheck(true);
                                        for (int i = items.size() - 1; i >= 0; i--) {
                                            if (items.get(i).getGuid().equals(bean.getGuid())) {
                                                items.remove(i);
                                                items.add(i, bean);
                                                break;
                                            }
                                        }
                                    }, System.out::println, () -> {
                                        slimAdapterEx.updateData(items);
                                    });

                        }
                    }
                });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }
}
